<?php

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Operator_MxShopPage extends WebPage {

    public function operatortype (){
        return  array('1' => '省级运营商', '2'=>'区级运营商');
    }

    public function main() {

        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;

        $condition = '';

        $types = $this->operatortype();

        $type = intval($_GPC['type']);
        if (!empty($type)) {
            $condition .= " AND c.type=" . $type;
        }

        $status = intval($_GPC['status']);
        if (!empty($status)) {

            if($status == 1){
                $status2 = 1;
            }else{
                $status2 = 0;
            }
            $condition .= " AND c.status=" . $status2;
        }

        if (!(empty($_GPC['searchfield'])) && !(empty($_GPC['keyword'])))
        {
            $paras[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            if ($_GPC['searchfield'] == 'member'){
                $condition .= ' AND (locate(:keyword,m.realname)>0 or locate(:keyword,m.mobile)>0)';

            }else if ($_GPC['searchfield'] == 'address'){
                $condition .= ' AND (locate(:keyword,c.province)>0 or locate(:keyword,c.city)>0 or locate(:keyword,c.dist)>0)';
            }
        }

        $selprov = trim($_GPC['selprov']);
        if (!empty($selprov)) {
            $city = pdo_fetchall("SELECT city FROM " . tablename('mx_shop_operator') . " WHERE uniacid=" . $uniacid . " AND province=:province AND deleted=0  GROUP BY city", array(':province' => $selprov));
            $condition .= ' AND c.province=:province';
            $paras[':province'] = $selprov;

            $selcity = trim($_GPC['selcity']);
            if (!empty($selcity)) {
                $dist = pdo_fetchall("SELECT dist FROM " . tablename('mx_shop_operator') . " WHERE uniacid=" . $uniacid . " AND city=:city AND deleted=0  GROUP BY dist", array(':city' => $selcity));
                $condition .= ' AND c.city=:city';
                $paras[':city'] = $selcity;

                $seldist = trim($_GPC['seldist']);
                if (!empty($seldist)) {
                    $condition .= ' AND c.dist=:dist';
                    $paras[':dist'] = $seldist;
                }
            }
        }

        $province = pdo_fetchall("SELECT province FROM " . tablename('mx_shop_operator') . " WHERE uniacid=" . $uniacid . " AND deleted=0 GROUP BY province");

        $list = pdo_fetchall("SELECT c.*, m.id as mid, m.realname, m.avatar FROM " . tablename('mx_shop_operator') . " c LEFT JOIN " . tablename('mx_shop_user') . " m ON c.uid=m.id WHERE c.uniacid = '{$_W['platformid']}' " . $condition . " AND c.deleted=0 ORDER BY c.displayorder DESC,c.id ASC LIMIT " . ($pindex - 1) * $psize . ",{$psize}", $paras);
        foreach ($list as $key=>$val){
            if($val['type'] == 1){
                $list[$key]['mdnumber'] = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_mendian') . " WHERE uniacid = '{$_W['platformid']}' AND deleted=0 AND province='{$val['province']}'" );
            }else{
                $list[$key]['mdnumber'] = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_mendian') . " WHERE uniacid = '{$_W['platformid']}' AND deleted=0 AND province='{$val['province']}' AND city='{$val['city']}' AND dist='{$val['dist']}'" );
            }
        }


        $total = pdo_fetchcolumn("SELECT count(c.id) FROM " . tablename('mx_shop_operator') . " c LEFT JOIN " . tablename('mx_shop_user') . " m ON c.uid=m.id WHERE c.uniacid = '{$_W['platformid']}' AND c.deleted=0 " . $condition, $paras);
        $pager = pagination($total, $pindex, $psize);

        include $this->template();

    }
    
    public function post(){
        
        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $types = $this->operatortype();

        $id = intval($_GPC['id']);

        if (!empty($id)) {
            $item = pdo_fetch("SELECT * FROM " . tablename('mx_shop_operator') . " WHERE id = :id AND deleted=0", array(':id' => $id));

            if (!(empty($item['uid'])))
            {
                $uid = m('userinfo')->getUser($item['uid']);
            }
            
            if($item['type'] == 2){
                $city = pdo_fetchall("SELECT city FROM " . tablename('mx_shop_mendian') . " WHERE uniacid=" . $uniacid . " AND province=:province AND deleted=0  GROUP BY city", array(':province' => $item['province']));
                $dist = pdo_fetchall("SELECT dist FROM " . tablename('mx_shop_mendian') . " WHERE uniacid=" . $uniacid . " AND city=:city AND deleted=0  GROUP BY dist", array(':city' => $item['city']));
            }

        } else {
            $item = array(
                'displayorder' => 0,
                'status' => 1,
            );
        }

        $province = pdo_fetchall("SELECT province FROM " . tablename('mx_shop_mendian') . " WHERE uniacid=" . $uniacid . " AND deleted=0 GROUP BY province");

        if ($_W['ispost']) {

            if (empty($_GPC['uid'])) {
                show_json(0,"请选择运营商");
            }

            if (empty($_GPC['type'])) {
                show_json(0,"请选择运营商级别");
            }

            if (empty($_GPC['province'])) {
                show_json(0,"请选择区域");
            }

            $condition = 'uniacid=:uniacid AND id != :id AND deleted=0 AND type=:type AND province=:province';
            $params = array(
                ':uniacid' => $_W['platformid'],
                ':id' => $id,
                ':type' => intval($_GPC['type']),
                ':province' => $_GPC['province'],
            );
            if(intval($_GPC['type'] == 2)){
                $condition .= ' AND city=:city AND dist=:dist';
                $params[':city'] = $_GPC['city'];
                $params[':dist'] = $_GPC['dist'];
                if($_GPC['dist'] == '市辖区'){
                    show_json(0,"请选择市区");
                }
            }

            $opid = pdo_fetch("SELECT id FROM " . tablename('mx_shop_operator') . " WHERE ".$condition, $params);
            if(!(empty($opid['id']))){
                show_json(0,"该区域已有运营商");
            }

            $data = array(
                'uniacid' => $uniacid,
                'displayorder' => intval($_GPC['displayorder']),
                'province' => $_GPC['province'],
                'type' => $_GPC['type'],
                'uid' => intval($_GPC['uid']),
                'status' => intval($_GPC['status']),
                'updatetime' => TIMESTAMP,
            );

            if($_GPC['type'] == 1){
                $data['city'] = 0;
                $data['dist'] = 0;
            }else{
                $data['city'] = $_GPC['city'];
                $data['dist'] = $_GPC['dist'];
            }

            if (empty($id)) {

                $data['createtime'] = TIMESTAMP;
                pdo_insert('mx_shop_operator', $data);
                $id = pdo_insertid();
                plog('mendian.operator.post', '新建运营商信息成功！运营商ID为' . $id);
            } else {
                pdo_update('mx_shop_operator', $data, array('id' => $id));
                plog('mendian.operator.post', '更新运营商信息成功！运营商ID为' . $id);
            }

            show_json(1,array('url'=>webUrl('mendian/operator')));

        }

        include $this->template();

    }

    public function status(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        $status = intval($_GPC['status']);

        if(!empty($id)){
            if($status == 1){
                pdo_update('mx_shop_operator', array( 'status' => 1), array('id' => $id));
            }else{
                pdo_update('mx_shop_operator', array( 'status' => 0), array('id' => $id));
            }
        }
        show_json(1, array('url'=>webUrl('mendian/operator')));

    }

    public function delete(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        if (empty($id)) {
            $id = is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0;
        }

        $cates = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_operator') . " WHERE id in( $id ) AND uniacid=" . $_W['platformid']);
        foreach ($cates as $cate) {
            pdo_update('mx_shop_operator', array('deleted' => 1, 'status' => 0), array('id' => $cate['id']));
            plog('mendian.operator.delete', "删除运营商 <br/>运营商信息:  ID: {$cate['id']}");
        }
        show_json(1, array('url' => referer()));

    }

    public function opajax(){

        global $_W,$_GPC;

        $str = trim($_GPC['str']);
        $foo = trim($_GPC['foo']);

        $data = array(
            'status' =>0,
        );

        if(!empty($str) && !empty($foo)){

            if($foo == 'city'){

                $sql = "SELECT city FROM " . tablename('mx_shop_operator') . "WHERE province=:province GROUP BY city";
                $arr = array(
                    ':province' => $str
                );
                $data['data'] = pdo_fetchall($sql, $arr);

            } elseif ($foo == 'dist'){

                $sql = "SELECT dist FROM " . tablename('mx_shop_operator') . "WHERE city=:city GROUP BY dist";
                $arr = array(
                    ':city' => $str
                );
                $data['data'] = pdo_fetchall($sql, $arr);

            }

            if(!empty($data['data'])){
                $data['status'] = 1;
            }

        }

        echo json_encode($data);
    }

}
